using Cysharp.Threading.Tasks;
using DG.Tweening;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIAlertItem : UIBaseNode
    {
        #region Template Generate,don't modify
        protected partial class UIB_UIAlertItem
        {
            #region ObjectBinding Generate 
            public GameObject normal { protected set; get; }
            public GameObject max { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage progress { protected set; get; }
            public UnityEngine.CanvasGroup main { protected set; get; }
            protected virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("normal", out var __tbv0);
                this.normal = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("max", out var __tbv1);
                this.max = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("progress", out var __tbv2);
                this.progress = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.CanvasGroup>("main", out var __tbv3);
                this.main = __tbv3;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify
        protected UIB_UIAlertItem ui { get; set; }
        public bool isHiding { get; private set; }

        private Transform _followTarget;
        private Vector3 _offsetWorld;
        private Vector2 _uiOffset;

        /// <summary>
        /// 可以自定义修改的
        /// </summary>
        protected partial class UIB_UIAlertItem
        {
            public virtual void StartBinding(GameObject __go)
            {
                var binding = __go.GetComponent<ObjectBinding>();
                if (binding != null)
                {
                    this.InitBinding(binding);
                }
            }
        }

        protected override void BeforeInit()
        {
            ui = new UIB_UIAlertItem();
            ui.StartBinding(gameObject);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            CancelHide();
            _followTarget = null;
            _offsetWorld = default;
            _uiOffset = default;
        }

        public void SetAlert(int alertValue)
        {
            ui.normal.SetActive(alertValue < 100);
            ui.max.SetActive(alertValue >= 100);
            ui.progress.fillAmount = (float)alertValue / 100;
        }

        public void SetFllowTarget(Transform follow, Vector3 offsetWorld, Vector2 uiOffset)
        {
            this._followTarget = follow;
            this._offsetWorld = offsetWorld;
            this._uiOffset = uiOffset;
            RefreshPosition();
        }

        public void RefreshPosition()
        {
            if (this._followTarget == null)
            {
                return;
            }
            Vector2 anchorPos = ManagerCenter.UI.World2UIPosition(this._followTarget.position + this._offsetWorld);
            this.transform.anchoredPosition = anchorPos + _uiOffset;
        }

        public async UniTask TweenHide()
        {
            isHiding = true;
            await ui.main.DOFade(0, 1).SetTarget(ui.main).SetLink(this.gameObject)
                .OnUpdate(RefreshPosition)
                .AwaitForComplete();
            ui.main.alpha = 1;
            isHiding = false;
        }

        public void CancelHide()
        {
            ui.main.DOKill();
            ui.main.alpha = 1;
            isHiding = false;
        }

        protected override void OnDispose()
        {

        }
    }
}
